<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>客户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <script src="../../../../res/js/jquery-3.4.1.min.js" charset="utf-8"></script>
    <script src="../../../../res/js/login_common.js" charset="utf-8"></script>
    <script src="../../../../res/layui/layui.js" charset="utf-8"></script>
    <script src="../../../../res/js/Tools.js"></script>
    <link rel="stylesheet" href="../../../../res/layui/css/layui.css" media="all">
</head>
<script>
    $.ajaxSetup({
        async: false
    });
    $(function () {
        $(".layui-bg-red").click(function () {
            $("#tsone").html("");
            $("#tstwo").html("");
        });

    })
</script>
<body>
<form class="layui-form layui-form-pane" id="formDId" action="" method="post" enctype="multipart/form-data">
    <input type="hidden" name="pkCarId" id="pkCarId"/>
    <!--<div class="layui-form-item">
        <label class="layui-form-label">车辆类型</label>
        <div class="layui-input-block">
            <input type="text" name="clientName" id ="clientName" lay-verify="required" autocomplete="off" placeholder="请输入车辆类型"
                   class="layui-input">
        </div>
    </div>-->

    <div class="layui-form-item">
        <label class="layui-form-label" style="width: 120px;">车辆名称</label>
        <div class="layui-input-block" style="margin-left: 120px">
            <input type="text" name="carName" id="carName" autocomplete="off" class="layui-input"
                   lay-verify="required" placeholder="请输入车辆名称">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">车辆类型</label>
        <div class="layui-input-block">
            <div id="carTypeId" class="xm-select-demo"></div>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">车牌</label>
        <div class="layui-input-block">
            <input type="text" name="carPlate" placeholder="请输入车牌" lay-verify="required" id="carPlate"
                   autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">厂牌</label>
        <div class="layui-input-block">
            <input type="text" name="carBrand" id="carBrand" autocomplete="off" lay-verify="required"
                   class="layui-input" placeholder="请输入厂牌">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">生产日期</label>
            <div class="layui-input-inline">
                <input type="text" name="carTime" id="carTime" lay-verify="date" placeholder="yyyy-MM-dd"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">购入日期</label>
            <div class="layui-input-inline">
                <input type="text" name="carPurchase" id="carPurchase" lay-verify="date" placeholder="yyyy-MM-dd"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block" id="flows">
            <button type="submit" class="layui-btn site-demo-active" lay-submit="" lay-filter="demo1">提交</button>
            <input type="button" id="demo3" class="layui-btn layui-btn-primary" value="取消">
        </div>
    </div>

    <script>

        //需要操作的数据
        let pkCarId = getQueryString("pkCarId");
        let carTypeId;
        let show = getQueryString("show");
        layui.use(['form', 'laydate', 'upload'], function () {
            var form = layui.form
                , laydate = layui.laydate;
            var $ = layui.jquery
                , upload = layui.upload;
            if (pkCarId) {
                let client = getAjaxById(getRootPath() + "OaCarAction/findById/" + pkCarId);
                console.info(client.data);
                carTypeId = client.data.carTypeId;
                //车辆id
                $("#pkCarId").val(client.data.pkCarId);
                //车辆名称
                $("#carName").val(client.data.carName);
                //车牌
                $("#carPlate").val(client.data.carPlate);
                //厂牌
                $("#carBrand").val(client.data.carBrand);
                //生产日期
                $("#carTime").val(dateFormat("yyyy-MM-dd", new Date(client.data.carTime)));
                //购入日期
                $("#carPurchase").val(dateFormat("yyyy-MM-dd", new Date(client.data.carPurchase)));
            }
            if(show){
                $("#flows").hide();
                $("#carName").attr("disabled", "");
                $("#carPlate").attr("disabled", "");
                $("#carBrand").attr("disabled", "");
                $("#carTime").attr("disabled", "");
                $("#carPurchase").attr("disabled", "");

            }

            //生产日期
            laydate.render({
                elem: '#carTime'
            });

            //购入日期
            laydate.render({
                elem: '#carPurchase'
            });

            //加载组件
            layui.config({
                //引入扩展文件库
                base: getRootPath() + 'res/layui/dist/'
            }).extend({
                //引入对应的文件名
                xmSelect: 'xm-select'
                //拿到对应的文件模块
            }).use(['xmSelect'], function () {
                //对于模块赋值使用
                var xmSelect = layui.xmSelect;
                //渲染多选
                pkJurisdictionId = xmSelect.render({
                    el: '#carTypeId',
                    radio: true,
                    layVerify: 'required',
                    tree: {
                        show: true,
                        strict: false,
                        expandedKeys: [-1],
                    },
                    name: 'carTypeId',
                    height: 'auto',
                    data: loadOaClientType(carTypeId, show)
                })

            });

            function loadOaClientType(id, show) {
                let url = getRootPath() + "OaCarTypeAction/loadCarType";
                let data = {
                    id: id, //需要操作的数据
                    show: '2'
                };
                let res = getAjax(url, data);
                if (res) {
                    return res;
                } else {
                    return [];
                }

            }


            //监听起草提交
            form.on('submit(demo1)', function (data) {

                console.log(data.field);
                // 单击之后提交按钮不可选,防止重复提交
                var DISABLED = 'layui-btn-disabled';
                // 增加样式
                $('.site-demo-active').addClass(DISABLED);
                // 增加属性
                $('.site-demo-active').attr('disabled', 'disabled');
                /*var formData = new FormData(document.getElementById("formDId"));
                console.log(formData)*/
                let res = postAjax(getRootPath() + "OaCarAction/addOrUpdate", data.field);
                if (res.code === 1) {
                    layer.msg('新增成功3秒后关闭当前窗口', {icon: 1});
                    setTimeout(function () {
                        formClose();
                    }, 3000);
                    setTimeout(function () {
                        parent.layui.table.reload('test');//刷新父页面
                        //window.parent.location.reload();//刷新父页面
                        const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                        parent.layer.close(index);
                    }, 3000);
                    //window.location.reload();//刷新页面
                } else if (res.code === 500) {
                    error('500');
                } else {
                    layer.msg("新增失败");
                }
                return false;
            });

            $("#demo3").on("click", function () {
                //询问框
                layer.confirm('是否关闭该页面？', {
                    btn: ['是的', '取消'] //按钮
                }, function () {
                    formClose();
                    parent.layui.table.reload('test');//刷新父页面
                    //window.parent.location.reload();//刷新父页面
                    const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                });
            });
        });


    </script>
</form>
</body>
</html>